//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Diagnostics.Contracts;

namespace OpenLS.Spreadsheet
{
    static class PasteManager
    {
        public static Range GetPasteTarget(Range source, Range target)
        {
            Contract.Requires(source != null);
            Contract.Requires(target != null);
            Contract.Ensures(Contract.Result<Range>() != null);
            Contract.Ensures(Contract.Result<Range>().Worksheet == target.Worksheet);
            if (source is FullColumnRange)
            {
                if (target is FullColumnRange)
                    return target;
                if (target is CellsRange)
                {
                    CellsRange cellsRange = (CellsRange)target;
                    if (cellsRange.TopLeftCell.Row.Index != 0)
                        throw new ApplicationException("invalid shape");
                    return new FullColumnRange(target.Worksheet.Columns[cellsRange.TopLeftCell.Column.Index],
                        target.Worksheet.Columns[cellsRange.TopLeftCell.Column.Index + target.ColumnSpan - 1]);
                }
            }
            if (source is FullRowRange)
            {
                if (target is FullRowRange)
                    return target;
                if (target is CellsRange)
                {
                    CellsRange cellsRange = (CellsRange)target;
                    if (cellsRange.TopLeftCell.Column.Index != 0)
                        throw new ApplicationException("invalid shape");
                    return new FullRowRange(target.Worksheet.Rows[cellsRange.TopLeftCell.Row.Index],
                        target.Worksheet.Rows[cellsRange.TopLeftCell.Row.Index + target.RowSpan - 1]);
                }
            }
            return target;
        }
    }
}
